


template <typename BVType>
BVTree<BVType>::BVTree(std::vector<Triangle> tris, size_t leafSz)
{
	m_nNumNode = 1;
	m_nMaxDepth = 1;
	pRoot= new TreeNode();
	pRoot->pBV = new BVType();
	pRoot->pBV->Build(tris);
	pRoot->pLeftNode = NULL;
	pRoot->pRightNode = NULL;
	BuildTree(pRoot,leafSz,0);
	std::cout << "Number of Nodes: "<<  m_nNumNode << std::endl;
	std::cout << "Depth Of Hierarchy: " <<m_nMaxDepth << std::endl;
}

template <typename BVType>
BVTree<BVType>::~BVTree(void)
{
DeleteAllNodes(pRoot);
}

template <typename BVType>
void BVTree<BVType>::BuildTree(TreeNode *current,size_t leafSz,int currentDepth)
{
	if(current->pBV->m_vTriangles.size() > leafSz)
	{
		std::vector<BVType*> kids;
		BVType * kid1 = new BVType();
		kids.push_back(kid1);
		BVType * kid2 = new BVType();
		kids.push_back(kid2);
		Split(current->pBV, kids);
		current->pLeftNode = new TreeNode();
		current->pRightNode = new TreeNode();
		currentDepth++;
		if(m_nMaxDepth < currentDepth)
			m_nMaxDepth = currentDepth;
		current->pLeftNode->pBV = kids[0];
		current->pRightNode->pBV = kids[1];
		m_nNumNode += 2;
		BuildTree(current->pLeftNode,leafSz,currentDepth);
		BuildTree(current->pRightNode,leafSz,currentDepth);
	}

}

template <typename BVType>
void BVTree<BVType>::Traverse(const TreeNode *node) /*,TraverseFunc func)*/
{
	if(true/*func(node->pBV)*/)
	{	
		if(node->pLeftNode != NULL)
		Traverse(node->pLeftNode,func);
		if(node->pRightNode != NULL)
		Traverse(node->pRightNode,func);
	}

}
template <typename BVType>
void BVTree<BVType>::DeleteAllNodes( TreeNode *node )
{
		if(node->pLeftNode != NULL)
		DeleteAllNodes(node->pLeftNode);
		if(node->pRightNode != NULL)
		DeleteAllNodes(node->pRightNode);
		delete node->pBV;
		delete node;
}